package base.day08;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class study03 {
    public static void main(String[] args) {
        Random rd = new Random();
        int number = rd.nextInt(100);

        JFrame jf = new JFrame();
        jf.setTitle("猜数字");
        jf.setSize(400, 300);
        jf.setDefaultCloseOperation(3);
        jf.setLocationRelativeTo(null);
        jf.setAlwaysOnTop(true);
        jf.setLayout(null);

        //文字说明
        JLabel gameRules = new JLabel("系统产生了一个1-100之间的随机数，请猜一猜");
        gameRules.setBounds(70, 50, 350, 20);
        jf.add(gameRules);

        //显示输入的文本框
        JTextField enterField = new JTextField();
        enterField.setBounds(120, 100, 150, 20);
        jf.add(enterField);

        //显示按钮
        JButton button = new JButton("猜测");
        button.setBounds(150, 150, 100, 20);
        jf.add(button);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String stringnumber = enterField.getText().trim();
                if (stringnumber.equals("")) {
                    JOptionPane.showMessageDialog(jf, "猜测的数字不能为空！！！");
                    enterField.setText("");
                    return;
                }
                int guessnumber = Integer.parseInt(stringnumber);
                if (guessnumber == number) {
                    JOptionPane.showMessageDialog(jf, "恭喜你猜对了！");
                    enterField.setText("");
                } else {
                    if (guessnumber < number) {
                        JOptionPane.showMessageDialog(jf, "输入的数字太小");
                        enterField.setText("");
                    } else {
                        JOptionPane.showMessageDialog(jf, "输入的数字过大");
                        enterField.setText("");
                    }
                }
            }
        });

        jf.setVisible(true);
    }
}
